{extend name="$_admin_base_layout" /}

{block name="content"}


<div class="alert alert-info alert-warning">
    <h3>同步操作根据话题量大小决定，越多时间越长。期间请不要关闭</h3>
</div>
<div class="alert alert-info alert-dismissable">
    <botton id="syn_btn" class="btn btn-primary push-5-r push-10" type="button"><i class="fa fa-arrows-alt"></i> 话题同步
    </botton>
</div>
<div class="alert alert-info alert-success" id="success">

</div>
{/block}

{block name="script"}
<script src="__ADMIN_JS__/core/jquery.countTo.min.js"></script>
<script>
    jQuery(function () {
        App.initHelpers(['appear-countTo']);
    });
</script>
<script>
    $(function () {
        var btn = $('#syn_btn');

        btn.click(function () {
            if (btn.attr('disabled') == 'disabled'){
                return;
            }
           $(this).attr('disabled',true);
            //查询索引信息
            $('#success').append("<h4>正在查询索引信息</h4>");
            var response = request("{:url('indicesInfo')}");
            if (response['act'] == 'del') {
                $('#success').append("<h4>" + response['msg'] + "</h4>");
                var delR = request("{:url('delIndices')}");
                if (delR['act'] == 'create') {
                    $('#success').append("<h4>" + delR['msg'] + "</h4>");
                    var createR = request("{:url('createIndices')}");
                    if (createR['status'] == 1) {
                        $('#success').append("<h4>" + createR['msg'] + "</h4>");
                        syn()
                    }
                }
            } else if (response['act'] == 'create') {
                $('#success').append("<h4>" + response['msg'] + "</h4>");
                var createR = request("{:url('createIndices')}");
                if (createR['status'] == 1) {
                    $('#success').append("<h4>" + createR['msg'] + "</h4>");
                    syn()
                }
            }
        });
    })

    function request(url, data = {}) {
        var response = {};
        $.ajax({
            url: url,
            type: "POST",
            data: data,
            async: false,
            success: function (res) {
                console.log(res);
                response = res;
            },
            fail: function () {
                alert('错误');
            }
        });
        return response;
    }

    function syn() {
        //获取数据库信息
        var dataInfo = request("{:url('topicCount')}")
        if (dataInfo['status'] == 1) {
            $('#success').append("<h4>" + dataInfo['msg'] + "</h4>");
            var page = dataInfo['data']['page'];
            console.log(dataInfo);
            for (var i = 1; i <= page; i++) {
                var res = request("{:url('synTopic')}", {"page": i});
                if (res['status'] == 1) {
                    $('#success').append("<h4>" + res['msg'] + "</h4>");
                    if (i == page){
                        $('#success').append("<h4 style='color: red'>更新完成</h4>");
                    }
                } else {
                    break;
                }
            }
        }
    }
</script>
{/block}